package com.jian.ecommercial.controller;

import com.jian.ecommercial.service.ex.PasswordNotMathcException;
import com.jian.ecommercial.service.ex.ServiceException;
import com.jian.ecommercial.service.ex.UsernameNotFoundException;
import com.jian.ecommercial.util.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;

public class BaseController {
    @ExceptionHandler({ServiceException.class})
    public JsonResult<Void> handleException(Throwable e){
        JsonResult<Void> result = new JsonResult<>();

        if (e instanceof UsernameNotFoundException){
            result.setState(4000);
            String message = e.getMessage();
            result.setMessage(message);
        }else if (e instanceof PasswordNotMathcException){
            result.setState(4001);
            String messsage = e.getMessage();
            result.setMessage(messsage);
        }

        return result;
    }
}
